package basic.thread.juc.chapter2;
import java.util.concurrent.atomic.AtomicInteger;
/**
 *  原子变量:jdk1.5后java.util.concurrent.atomic 包下提供了常用的原子变量:
 *   1.volatile 保证内存可见性
 *   2.CAS(Compare-And-Swap)算法保证数据的原子性
 *     CAS 算法是硬件对于并发操作共享数据的支持
 *
 *
 *
 * */
public class TestAtomicDemo {
    public static void main(String[] args) {
        AtomicDemo atomicDemo = new AtomicDemo();
        for(int i=0;i<1000;i++){
           new Thread(atomicDemo).start();
        }
    }
}

class AtomicDemo implements Runnable{
    public AtomicInteger value = new AtomicInteger();
    @Override
    public void run() {
        try {
            Thread.sleep(200);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println(Thread.currentThread().getName()+"："+getValue());
    }

    public int getValue(){
        return value.getAndIncrement();
    }
}
